package com.example.tabbar.fragment

import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.CheckBox
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.tabbar.R
import com.example.tabbar.adapter.GroupAdapter
import com.example.tabbar.databinding.FragmentModeGroupBinding
import com.example.tabbar.room.entity.Group


class ModeGroupFragment : Fragment() {
    private var binding: FragmentModeGroupBinding? = null
    private var recyclerView: RecyclerView? = null
    private var adapter: GroupAdapter ? = null
    private var currentSelectAll: CheckBox? = null
    private var selectAll: CheckBox? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = FragmentModeGroupBinding.inflate(inflater, container, false)
        return binding!!.root
    }
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        recyclerView = binding!!.groupRecyclerView
        selectAll = binding!!.selectAll
        currentSelectAll = binding!!.currentSelectAll
        adapter = GroupAdapter()
        recyclerView!!.adapter = adapter
        recyclerView?.layoutManager = LinearLayoutManager(requireContext())
        recyclerView?.addItemDecoration(
            DividerItemDecoration(requireContext(),
                DividerItemDecoration.VERTICAL)
        )
        val list = createGroup()

        adapter?.setItems(list)

        //跳转设置页面
//        binding?.setting?.setOnClickListener {
//            val intent = Intent(Settings.ACTION_SETTINGS)
//            startActivity(intent)
//        }
        //统一监听器
        val checkBoxListener: (android.widget.CompoundButton, Boolean) -> Unit = { _, isChecked ->
            adapter?.selectAll(isChecked)
        }

        currentSelectAll?.setOnCheckedChangeListener(checkBoxListener)
        selectAll?.setOnCheckedChangeListener(checkBoxListener)

        adapter?.setOnItemClickListener { select ->
            selectAll?.setOnCheckedChangeListener(null)
            currentSelectAll?.setOnCheckedChangeListener(null)

            selectAll?.isChecked = select
            currentSelectAll?.isChecked = select

            currentSelectAll?.setOnCheckedChangeListener(checkBoxListener)
            selectAll?.setOnCheckedChangeListener(checkBoxListener)
        }

    }

    private fun createGroup() : List<Group> {
        val list =
        listOf(Group(1, "Group 1"), Group(2, "Group 2"), Group(3, "Group 3"), Group(4, "Group 4"))
        return list
    }
    fun loadFragment(fragment: Fragment,isReuse: Boolean = false) {

        // 使用hide/show代替replace提高性能
        val transaction = childFragmentManager.beginTransaction()

        // 隐藏所有Fragment
        childFragmentManager.fragments.forEach {
            transaction.hide(it)
        }

        // 显示目标Fragment或添加新Fragment
        if (fragment.isAdded) {
            transaction.show(fragment)
        } else {
            transaction.add(R.id.user_container, fragment)
            if (!isReuse && fragment !is UserListFragment) {
                transaction.addToBackStack(null)
            }
        }

        transaction.commit()
    }

    companion object {

        @JvmStatic
        fun newInstance() =
            ModeGroupFragment().apply {
                arguments = Bundle().apply {

                }
            }
    }
}